﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading;
using CommonLib;
using NetService;

namespace Ng
{
    internal sealed class ArticleProvider
    {
        public List<string> GetArticles(int count)
        {
            var result = new List<string>();

            var date = new DateTime(2010, 10, 10);

            do
            {
                GetArticleFromDate(date, result);

                date = date.AddDays(-1);

                Thread.Sleep(500);
            } while (result.Count < count);

            return result;
        }

        private void GetArticleFromDate(DateTime date, ICollection<string> result)
        {
            var dateStr = date.ToString("yyyy-MM-dd");
            var urlStr = string.Format(URL, dateStr);

            var page = PageService.GetPage(urlStr);

            if (string.IsNullOrWhiteSpace(page))
                return;

            var articles = GetArticleList(page);

            foreach (var article in articles.Where(article => !result.Contains(article)))
                result.Add(article);
        }

        private List<string> GetArticleList(string page)
        {
            int curPos = 0;
            var res = new List<string>();

            do
            {
                var index = page.IndexOf("class=\"huge\"", curPos);

                if (index == -1)
                    break;

                var aHrefStartIndex = page.IndexOf("<a href=", index - 50);

                var urlStartIndex = page.IndexOf('"', aHrefStartIndex);
                urlStartIndex++;
                var urlEndIndex = page.IndexOf('"', urlStartIndex);

                var length = urlEndIndex - urlStartIndex;

                if (length < 0)
                    break;

                var aHref = page.Substring(urlStartIndex, length);

                if (string.IsNullOrWhiteSpace(aHref))
                    break;

                var articleUrl = @"http://www.ng.ru" + aHref;
                res.Add(articleUrl);

                curPos = urlEndIndex + 20;

            } while (true);

            return res;
        }

        private const string URL = @"http://www.ng.ru/politics/{0}/";

        public WordStatArticle GetArticleData(string url)
        {
            var page = PageService.GetPage(url);

            if (string.IsNullOrWhiteSpace(page))
                return null;

            var text = GetText(page);

            if (text.Length < 1000)
                return null;

            var title = GetTitle(page);

            if (string.IsNullOrWhiteSpace(title))
                return null;

            var article = new WordStatArticle
                              {
                                  Date = DateTime.Now,
                                  Source = "Новая газета",
                                  Text = text,
                                  Title = title,
                                  Url = url
                              };

            return article;
        }

        private string GetTitle(string page)
        {
            var aTag = "<h1>";
            var startIndex = page.IndexOf(aTag);

            if (startIndex == -1)
                return string.Empty;

            var endIndex = page.IndexOf("</h1>", startIndex);

            if (endIndex == -1)
                return string.Empty;

            var length = endIndex - startIndex - (("</h1>").Length - 1);

            if (length < 0)
                return string.Empty;

            var name = page.Substring(startIndex + aTag.Length, length);

            return name;
        }

        private string GetAuthor1(string page)
        {
            var aTag = "class=\"author\">";
            var startIndex = page.IndexOf(aTag);

            if (startIndex == -1)
                return string.Empty;

            var endIndex = page.IndexOf("<", startIndex);

            if (endIndex == -1)
                return string.Empty;

            var length = endIndex - startIndex;

            if (length < 0)
                return string.Empty;

            var name = page.Substring(startIndex + aTag.Length, length);

            return name;
        }

        private string GetAuthor2(string page)
        {
            var aTag = "class=\"author\">";
            var startIndex = page.IndexOf(aTag);

            if (startIndex == -1)
                return string.Empty;

            var endIndex = page.IndexOf("<", startIndex);

            if (endIndex == -1)
                return string.Empty;

            var length = endIndex - startIndex;

            if (length < 0)
                return string.Empty;

            var name = page.Substring(startIndex + aTag.Length, length);

            return name;
        }

        private string GetText(string page)
        {
            var res = string.Empty;

            int curPos = 0;

            do
            {
                var startIndex = page.IndexOf("<p>", curPos, StringComparison.InvariantCultureIgnoreCase);

                if (startIndex == -1)
                    break;

                startIndex += 3;
                var endIndex = page.IndexOf("</p>", startIndex, StringComparison.InvariantCultureIgnoreCase);

                if (endIndex == -1)
                    break;

                curPos = startIndex + 5;

                if (startIndex == -1 || endIndex == -1)
                    break;

                var length = endIndex - startIndex;

                if (length < 0)
                    break;

                var paragraph = page.Substring(startIndex, length).Trim();

                if (string.IsNullOrWhiteSpace(paragraph))
                    continue;

                res += " " + paragraph;
            } while (true);

            return Regex.Replace(res, @"<[^>]*>", string.Empty);
        }
    }
}
